package request

import "github.com/astaxie/beego/validation"

/*
type ProductRuleRequest struct {
	Id        uint   `json:"id"`                                //主键
	RuleName  string `json:"ruleName" binding:"required"`       //属性名称 必填
	RuleValue string `json:"ruleValue" binding:"required"` //规格属性值 必填 规格值【JSON字符串】 [{\"detail\": [\"string\"],\"title\": \"string\"}]
}

func (p *ProductRuleRequest) Valid(v *validation.Validation) {
	if p.RuleName == "" {
		v.SetError("RuleName", "规格名称不能为空")
	} else if len(p.RuleName) > 32 {
		v.SetError("RuleName", "规格名称长度不能超过32个字符")
	} else if p.RuleValue == "" {
		v.SetError("RuleValue", "规格值不能为空")
	}
}
*/

/*
{
    "ruleName": "围巾",
    "spec": [
        {
            "value": "颜色",
            "detail": ["格物致知蓝咖","格物致知红色"]
        }
    ]
}

{
    "ruleName": "颜色",
    "spec": [
        {
            "value": "颜色",
            "detail": ["白色","黑色","粉色","红色"]
        }
    ]
}
*/
type ProductRuleRequest struct {
	Id       uint   `json:"id"`
	RuleName string `json:"ruleName"`
	Spec     []Spec `json:"spec"`
}

type Spec struct {
	Detail []string `json:"detail"`
	Value  string   `json:"value"`
}

func (p *ProductRuleRequest) Valid(v *validation.Validation) {
	if p.RuleName == "" {
		v.SetError("RuleName", "规格模板名称不能为空")
	} else if len(p.RuleName) > 32 {
		v.SetError("RuleName", "规格模板名称不能超过32个字符")
	}
}

func (s *Spec) Valid(v *validation.Validation) {
	if len(s.Detail) == 0 {
		v.SetError("Detail", "规格值不能为空")
	} else if s.Value == "" {
		v.SetError("Value", "规格名称不能为空")
	}
}
